package com.example.lgtandroidpwork;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.VideoView;

import com.github.javafaker.Faker;

import java.util.Locale;
import java.util.Random;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class MainActivity extends AppCompatActivity {
    private DanmakuView danmakuView ; //弹幕
    private boolean showDanmaku ; //弹幕数量
    private DanmakuContext danmakuContext ;  //弹幕库
    private LinearLayout ly_send ; //线性布局
    private Button btn_send ;//按钮
    private EditText edit_text ;//设置输入框
    private VideoView videoView ;//播放器


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        playVideo();
        initDanmaku();
    }
    /*
    初始化界面控件
    */
    private void initView() {
        videoView=findViewById(R.id.videoview);
        ly_send=findViewById(R.id.ly_send) ;
        btn_send=findViewById(R.id.btn_send) ;
        edit_text=findViewById(R.id.edit_text) ;
        danmakuView=findViewById(R.id.danmaku);

    }
    /*
     * 播放视频方法*/
    private void playVideo(){
        String uri="android.resource://"+getPackageName()+"/"+R.raw.sun ;
        if (uri !=null){
            videoView.setVideoURI(Uri.parse(uri));
            videoView.start();

        }else {
            videoView.getBackground().setAlpha(0);//将背景设为透明
        }
    }
    /*
     * 创建弹幕解析器
     * */
    private BaseDanmakuParser parser = new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };
    /*
     * 初始化弹幕*/
    private  void initDanmaku (){
        danmakuView.setCallback(new DrawHandler.Callback() {//设置回调方法
            @Override
            public void prepared() {
                showDanmaku =true;
                danmakuView.start();//开始弹幕
                generateDanmakus();//调用随机生成弹幕方法

            }
            @Override
            public void updateTimer(DanmakuTimer timer) {
            }
            @Override
            public void danmakuShown(BaseDanmaku danmaku) {
            }
            @Override
            public void drawingFinished() {
            }
        });
        danmakuContext =DanmakuContext.create() ;
        danmakuView.enableDanmakuDrawingCache(true);//提升屏幕绘制效率
        danmakuView.prepare(parser,danmakuContext);
        //为danmakuView设置点击事件
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ly_send.getVisibility()==View.GONE){
                    ly_send.setVisibility(View.VISIBLE);//显示布局
                }else {
                    ly_send.setVisibility(View.GONE);//隐藏布局
                }
            }
        });
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=edit_text.getText().toString().trim() ;
                if (!TextUtils.isEmpty(content)){
                    addDanmaku(content,true) ;//添加一条弹幕
                    edit_text.setText("");
                }

            }
        });
    }

    /**添加一条弹幕
     * @param content  弹幕是否具有内容
     * @param border  弹幕是否具有边框
     */

    private  void addDanmaku(String content ,boolean border){
        //创建弹幕对象，TYPE_SCROLL_RL表示从右边向左滚动的弹幕
        BaseDanmaku danmaku =danmakuContext.mDanmakuFactory.
                createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);//右→左
        danmaku.text=content ;
        danmaku.padding=6 ;
        danmaku.textSize=30 ;
        danmaku.textColor= Color.WHITE; //
        danmaku.setTime(danmakuView.getCurrentTime());
        if (border){

            danmaku.borderColor=Color.BLUE ;//弹幕文字边框的颜色
        }
        danmakuView.addDanmaku(danmaku);//添加一条弹幕
    }
    /**
     * 方法调用出随机生成弹幕
     * 随机生成一些弹幕
     * */
    private void generateDanmakus() {
        final Faker faker = new Faker(new Locale("zh-CN"));



        new Thread(new Runnable() {
            @Override
            public void run() {
                while (showDanmaku){
                    int num =new Random().nextInt(300);
                    String content =faker.name().name();//输出
                    addDanmaku(content,false);
                    try {
                        Thread.sleep(num);

                    }catch (InterruptedException e){
                        e.printStackTrace();

                    }
                }
            }
        }).start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (danmakuView != null && danmakuView.isPrepared()) {
            danmakuView.pause();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (danmakuView != null && danmakuView.isPrepared() &&
                danmakuView.isPaused()) {
            danmakuView.resume();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        showDanmaku = false;
        if (danmakuView != null) {
            danmakuView.release();
            danmakuView = null;

        }
    }


}
